Add codec factory registration and enhance type mapping functionality#102
Add codec factory registration and enhance type mapping functionality#102freehere107 merged 3 commits intomasterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the codec registry to register decoder factories (instead of shared instances), adds a codec cache for faster codec lookup/instantiation, and broadens encode support for more Go slice types while adding targeted tests/benchmarks.
Changes:
- Replace the reflection-based registry with
CodecFactoryregistration plus a concurrency-safe(module,spec,typeString)codec cache. - Expand encoding to accept more concrete slice types (e.g.,
[]uint32,[]int) via helper conversion utilities. - Add new unit tests/benchmarks for type mapping access, vec encoding, U256 encoding, and cache concurrency.
Reviewed changes
Copilot reviewed 24 out of 24 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| utiles/tools.go | Removes unused reflect-based nil helper and cleans imports. |
| types/v13.go | Registers OriginCaller via factory to avoid shared decoder state. |
| types/types_test.go | Adds assertions/tests for vec encoding slice types and U256 byte-slice encoding. |
| types/types.go | Updates multiple Encode signatures to interface{} and improves some decode/encode internals. |
| types/type_mapping_test.go | Adds test verifying Consensus exposes a TypeMapping via TypeMappingGetter. |
| types/registry_types.go | Adds generated list of base codec type names (input to registry generator). |
| types/registry_gen.go | Adds generated baseCodecFactories map (type name → factory). |
| types/registry.go | Implements factory-based registry, codec cache, and GetCodec API returning (Decoder, subType, error). |
| types/encode_helpers.go | Adds helpers to normalize many concrete slice types into []interface{} for encoders. |
| types/customType_test.go | Adds concurrency test covering codec cache + encode/decode under goroutines. |
| types/customType.go | Migrates custom type registration to factory functions; resets codec cache on registration. |
| types/benchmark_test.go | Adds benchmarks for decode/encode hot paths. |
| types/basic_types_test.go | Adds tests for Bytes/HexBytes/Option/Null/hash types encode/decode. |
| types/base.go | Introduces Decoder/Encoder interfaces, adds fast-path decoding for common primitives, updates encode path to use GetCodec. |
| types/Vectors.go | Refactors Vec.Encode to accept more slice types without reflection. |
| types/Uint.go | Refactors uint decode/encode (less allocation) and extends U256 encoding inputs. |
| types/Struct.go | Updates struct encoding to accept interface{} and type assert to map. |
| types/Results.go | Updates result encoding to accept interface{} and type assert to map. |
| types/Option.go | Replaces reflect-based nil detection with explicit checks for common typed-nil cases. |
| types/FixedArray.go | Refactors fixed array encoding to support more slice types without reflection. |
| types/Bytes.go | Updates Bytes/HexBytes/String encoders to accept interface{} + type assertion. |
| types/Bool.go | Updates Bool encoder to accept interface{} + type assertion. |
| tools/gen_registry/main.go | Adds generator that produces registry_gen.go from registry_types.go. |
| .github/workflows/ci.yml | Updates Go GitHub Actions to newer major versions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 24 out of 24 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
types/Option.go:56
Option.Encodeno longer treats typed-nil values (e.g.(*T)(nil),[]uint32(nil),map[string]string(nil), etc.) as empty options. Sincevalue == nilis false when an interface holds a typed-nil, these will currently fall through and be encoded asSome(...)(or panic forbool), which is a behavior change from the priorutiles.IsNilimplementation.
Consider restoring a general typed-nil check (e.g. a small helper using reflect.ValueOf(value).Kind() + IsNil() for ptr/map/slice/chan/func/interface) or otherwise broadening the nil detection so all typed-nil values encode as 00.
func (o *Option) Encode(value interface{}) string {
if v, ok := value.(string); ok && v == "" {
return "00"
}
if value == nil {
return "00"
}
switch v := value.(type) {
case []byte:
if v == nil {
return "00"
}
case []interface{}:
if v == nil {
return "00"
}
case map[string]interface{}:
if v == nil {
return "00"
}
case error:
if v == nil {
return "00"
}
}
if o.SubType == "bool" {
if value.(bool) {
return "01"
}
return "02"
}
return "01" + EncodeWithOpt(o.SubType, value, &ScaleDecoderOption{Spec: o.Spec, Metadata: o.Metadata})
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
No description provided.